#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
柔性作业车间调度问题主程序
实现FJSP问题的求解与重调度
"""

from config.config import *
from src.data_processor import DataProcessor
from src.fjsp_model import FlexibleJobShop
from src.multi_objective import MultiObjectiveOptimizer
from src.nsga2_algorithm import NSGA2


def main():
    """主函数：根据配置执行普通调度或重调度"""
    
    # 创建数据处理对象
    data_processor = DataProcessor()
    
    if SCHEDULING_MODE == 0:
        # === 普通调度流程 ===
        # 1. 数据处理
        info, delivery_time, work = data_processor.read_first(MACHINE_FILE)
        order_num, machine_num = data_processor.get_order_machine_count()
        parm_data = [info, delivery_time, work]

        # 2. 数据初始化与求解
        fjsp = FlexibleJobShop(
            SCHEDULING_MODE, POWER_FILE, order_num, machine_num, 
            SELECT_MIN_MACHINE_PROB, parm_data
        )
        optimizer = MultiObjectiveOptimizer()
        nsga2 = NSGA2(
            SCHEDULING_MODE, ITERATIONS_NUM, POPULATION_SIZE, 
            fjsp, optimizer, work, order_num
        )
        
        # 3. 求解与结果输出
        job, machine, machine_time, fit_every = nsga2.run1()  # 最后一次迭代的最优解
        # 绘制目标函数变化图
        optimizer.draw_objective_changes1(fit_every)  # 每次迭代过程中pareto解中3个目标的变化
        fjsp.get_result(job, machine, machine_time, COLOR_FILE, FIRST_SCHEDULING_RESULT)
        
        print(f"普通调度完成，结果已保存到 {FIRST_SCHEDULING_RESULT}")
    # ------------------------------------------------------------------------------------------------------------------
    elif SCHEDULING_MODE == 1:
        # === 重调度流程 ===
        # 1. 数据处理
        info, delivery_time, work, jobdic, jobing, insertpoint, nojob = data_processor.read_Rescheduling(
            MACHINE_FILE, FIRST_SCHEDULING_RESULT, INSERT_ORDER_FILE)
        order_num, machine_num = data_processor.get_order_machine_count()
        parm_data = [info, delivery_time, work]

        # 2. 数据初始化与求解
        fjsp = FlexibleJobShop(
            SCHEDULING_MODE, POWER_FILE, order_num, machine_num, 
            SELECT_MIN_MACHINE_PROB, parm_data, 
            jobing, insertpoint, jobdic, nojob
        )
        optimizer = MultiObjectiveOptimizer()
        nsga2 = NSGA2(
            SCHEDULING_MODE, ITERATIONS_NUM, POPULATION_SIZE, 
            fjsp, optimizer, work, order_num
        )
        
        # 3. 求解与结果输出
        job, machine, machine_time, fit_every = nsga2.run2()
        # 绘制目标函数变化图
        optimizer.draw_objective_changes2(fit_every)
        fjsp.get_result(job, machine, machine_time, COLOR_FILE, RESCHEDULING_RESULT)
        
        print(f"重调度完成，结果已保存到 {RESCHEDULING_RESULT}")
    # ------------------------------------------------------------------------------------------------------------------

    else:
        # === 2 预测性维护重调度流程 ===
        # 1. 数据处理
        reinfo, delivery_time, work, jobdic, jobing, MAINTENANCE_TIME, nojob = data_processor.read_MaintenanceRescheduling(
            MACHINE_FILE, FIRST_SCHEDULING_RESULT)
        order_num, machine_num = data_processor.get_order_machine_count()
        parm_data = [reinfo, delivery_time, work]

        # 2. 数据初始化与求解
        fjsp = FlexibleJobShop(
            SCHEDULING_MODE, POWER_FILE, order_num, machine_num,
            SELECT_MIN_MACHINE_PROB, parm_data,
            jobing, MAINTENANCE_TIME, jobdic, nojob
        )
        optimizer = MultiObjectiveOptimizer()
        nsga2 = NSGA2(
            SCHEDULING_MODE, ITERATIONS_NUM, POPULATION_SIZE,
            fjsp, optimizer, work, order_num
        )

        # 3. 求解与结果输出
        job, machine, machine_time, fit_every = nsga2.run2()
        # 绘制目标函数变化图
        optimizer.draw_objective_changes2(fit_every)
        fjsp.get_result(job, machine, machine_time, COLOR_FILE, RESCHEDULING_RESULT)

        print(f"重调度完成，结果已保存到 {RESCHEDULING_RESULT}")

if __name__ == "__main__":
    main()
